package com.shiroha.user.controller;

import com.shiroha.common.utils.Result;
import com.shiroha.user.pojo.Follow;
import com.shiroha.user.pojo.vo.FollowResponse;
import com.shiroha.user.service.IFollowService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("/us")
@AllArgsConstructor
public class FollowController {

    private final IFollowService followService;

    // 关注用户
    @PostMapping("/follows")
    public Result<String> followUser(@AuthenticationPrincipal Jwt jwt, @RequestParam String userId) {
        // 关注用户为 follower, 发起关注用户为 followee
        followService.saveFollow(UUID.fromString(userId), UUID.fromString(jwt.getSubject()));
        return Result.success("关注成功");
    }

    // 取消关注
    @DeleteMapping("/follows")
    public Result<String> unfollowUser(@AuthenticationPrincipal Jwt jwt, @RequestParam String userId) {
        boolean isSuccess = followService.removeFollow(UUID.fromString(userId), UUID.fromString(jwt.getSubject()));
        if(isSuccess) {
            return Result.success("取消关注成功");
        } else {
            return Result.error("取消关注失败");
        }
    }

    // 获取粉丝列表
    @GetMapping("/followers")
    public ResponseEntity<FollowResponse> getFollowersByUserId(@AuthenticationPrincipal Jwt jwt) {
        UUID userId = UUID.fromString(jwt.getSubject());
        return ResponseEntity.ok(followService.findFollowersByUserId(userId));
    }

    // 获取关注列表
    @GetMapping("/followees")
    public ResponseEntity<FollowResponse> getFolloweesByUserId(@AuthenticationPrincipal Jwt jwt) {
        UUID userId = UUID.fromString(jwt.getSubject());
        return ResponseEntity.ok(followService.findFolloweesByUserId(userId));
    }

    // 获取朋友（互关）列表
    @GetMapping("/friends")
    public ResponseEntity<FollowResponse> getFriendsByUserId(@AuthenticationPrincipal Jwt jwt) {
        UUID userId = UUID.fromString(jwt.getSubject());
        return ResponseEntity.ok(followService.findFriendsByUserId(userId));
    }
}
